import Vue from 'vue';
import lodash from 'lodash';
const urlReg = new RegExp('^((http|https):\/\/)?(([A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\.)+([A-Za-z]+)[/\?\:]?.*$');
export class menuToRouter {
	constructor(menu) {
		let menuArr = Vue.prototype.$treeToFlat(menu, 'childList');
		return this.formatRouter(menuArr.filter(n => n.level !== '3'));
	}
	formatRouter(arr) {
		let dataArr = arr.map(item => {
			// let urlArr = item.url.split('/');
			let obj = lodash.pick(item, ['id', 'pid', 'icon', 'level', 'isPublic']);
			return { ...obj, title: item.name, path: item.url, isUrl: urlReg.test(item.url) };
		});
		console.log(dataArr.filter(n => n.isPublic === '1'));
		return Vue.prototype.$flatToTree(dataArr.filter(n => n.isPublic === '1'), 'pid');
	}
}